<?php
require_once('../../../wp-config.php');
require_once('../../../wp-includes/functions.php');
$error = "";
$output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";;
$output .= '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" style="height: 100%">'."\n";;
$output .= '<head>'."\n";;
$output .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";;
$output .= '<title>Google Maps</title>'."\n";;

require_once(dirname(__FILE__).'/xmlgooglemaps_googleMapsAPI.php');
require_once(dirname(__FILE__).'/xmlgooglemaps_helper.php');

$id = -1;
$fullscreen=0;
if (isset($_GET["gpxid"])) {
	$id = intval($_GET["gpxid"]);
} else {
	if (isset($_GET["kmlid"])) {
		$id = intval($_GET["kmlid"]);
	} else {
		if (isset($_GET["mygooglemapid"])) {
			$id = intval($_GET["mygooglemapid"]);
		} else {
			if (isset($_GET["myid"])) {
				$id = intval($_GET["myid"]);
			}
		}
	}
}
$fullscreen=0;
if (isset($_GET["fullscreen"])) {
	$fullscreen = intval($_GET["fullscreen"]);
}

$varstring = dbFunctions::getVarString($id);
$params = new xml_gm_params();
$params->setParams($varstring);

$cacheKey = "map".$fullscreen;
$cache = dbFunctions::getCache($id,$cacheKey,$params->cacheDuration);
if ($cache === false) {
	$googlemaps = new googleMapAPI_PMPro($params->googleMapsAPIKey);
	$googlemaps->setFullscreen($fullscreen);
	$googlemaps->width = "100%";
	$googlemaps->height = "100%";
	$googlemaps->setPanZoomControl($params->panZoomControl);
	$googlemaps->setOverviewMap($params->overViewMapControl);
	$googlemaps->setScaleControl($params->scaleControl);
	$googlemaps->setMapTypeControl($params->mapTypeControl);
	$googlemaps->setMapTypeControlButton("G_NORMAL_MAP",$params->mapNormal);
	$googlemaps->setMapTypeControlButton("G_HYBRID_MAP",$params->mapHybrid);
	$googlemaps->setMapTypeControlButton("G_PHYSICAL_MAP",$params->mapPhysical);
	$googlemaps->setMapTypeControlButton("G_SATELLITE_MAP",$params->mapSatellite);
	$googlemaps->setMapTypeControlButton("G_SATELLITE_3D_MAP",$params->mapEarth);
	$googlemaps->setMapType($params->viewStyle);
	$googlemaps->setContinuousZoom($params->continuousZoom);
	$googlemaps->setDragging($params->dragging);
	$googlemaps->setScrollWheelZoom($params->scrollWheelZoom);
	$googlemaps->setDoubleClickZoom($params->doubleClickZoom);
	$googlemaps->setGoogleBar($params->googleBar);

	//NGGallery Feature if Images are Geocoded
	if ($params->getNggCount()>0) {
		$ngg = new nggFunctions();
		if ($ngg->isNggInstalled()) {
			if ($ngg->isExifInstalled()) {
				$rows = $ngg->getNggPics($params->nggalGalleries,$params->nggalPics);
				foreach ($rows as $row) {
					$creationdate = date_i18n(get_option('date_format').' '.get_option('time_format'), $row["creation"]);
					$lat = myFunctions::latitude2Text($row["gpslat"]);
					$lon = myFunctions::longitude2Text($row["gpslon"]);
					$html = $params->nggalMarkerTemplate;
					$html = str_replace("%FULLPATH%",$row["path"],$html);
					$html = str_replace("%THUMBPATH%",$row["thumbpath"], $html);
					$html = str_replace("%TITLE%",$row["title"],$html);
					$html = str_replace("%DESCRIPTION%", $row["description"], $html);
					$html = str_replace("%CREATIONDATE%", $creationdate, $html);
					$html = str_replace("%LATITUDE%", $lat, $html);
					$html = str_replace("%LONGITUDE%", $lon, $html);
					$googlemaps->addMarker($row["gpslat"],$row["gpslon"],$html,"","",$row["thumbpath"],$row["thumbpath"],32,32,32,32,16,16);
				}
			} else {
				$error .= "Your PHP installation on your webserver doesn&apos;t support EXIF functions. This plugin needs these functions to read the coordinates from the image file. Please consult <a href=\"http://ch2.php.net/manual/en/book.exif.php\" target=\"_blank\">http://ch2.php.net/manual/en/book.exif.php</a> for more information!<br />";
			}
		} else {
			$error .= "NextGEN Gallery Plugin is not installed or activated!<br />";
		}
	}

	$output .= $googlemaps->getHeaderScript().''."\n";;

	$bannerbottom = max(0,min($params->height - 175, 50));

	$output .= '<style type="text/css">'."\n";;
	$output .= 'p.entry { '.$params->gpxCSSStyle.'}'."\n";;
	$output .= '</style>'."\n";;
	$output .= '</head>'."\n";;
	$output .= '<body style="margin: 0; height: 100%; overflow: hidden;" onunload="GUnload();">'."\n";;

	//GPX Files!
	if (isset($_GET["gpxid"])) {
		require_once(dirname(__FILE__).'/xmlgooglemaps_gpxParser.php');
		$url = dbFunctions::getPath($id);
		$gpxfile = new gpxParser();
		$gpxfile->parse($url);

		$gpxdata = $gpxfile->output;

		$maxwpt = $gpxdata["wpt_count"];
		$counter = 0;

		if (($params->gpxView == "all") || ($params->gpxView == "wpt")) {
			for ($counter=0; $counter<$maxwpt; $counter++) {
				$desc = $params->gpxWaypointMarkerTemplate;
				$lat = myFunctions::latitude2Text($gpxdata["wpt".$counter]["lat"]);
				$lon = myFunctions::longitude2Text($gpxdata["wpt".$counter]["lon"]);
				$elevation = myFunctions::elevation2Text($gpxdata["wpt".$counter]["ele"],$params->measurement);
				$desc = str_replace("%NAME%",$gpxdata["wpt".$counter]["name"],$desc);
				$desc = str_replace("%DESCRIPTION%",$gpxdata["wpt".$counter]["desc"],$desc);
				$desc = str_replace("%LINK%",$gpxdata["wpt".$counter]["link"],$desc);
				$desc = str_replace("%LATITUDE%", $lat, $desc);
				$desc = str_replace("%LONGITUDE%", $lon, $desc);
				$desc = str_replace("%ELEVATION%", $elevation, $desc);
				$googlemaps->addMarker($gpxdata["wpt".$counter]["lat"],$gpxdata["wpt".$counter]["lon"],$desc,$gpxdata["wpt".$counter]["link"],$params->gpxDirectOpenWPLink);
			}
		}
		if (($params->gpxView == "all") || ($params->gpxView == "rte")) {
			$rtemax = $gpxdata["rte_count"];
			for ($rtecount=0;$rtecount<$rtemax; $rtecount++) {
				$rteptmax=$gpxdata["rte".$rtecount]["rtept_count"];
				$pl = $googlemaps->addPolyline($params->gpxLineColor,$params->gpxLineWidth,$params->gpxLineOpacity);

				for ($rteptcount=0;$rteptcount<$rteptmax; $rteptcount+=$params->gpxInterval) {
					$pt = $gpxdata["rte".$rtecount]["rtept".$rteptcount];
					$googlemaps->addPolylinePoint($pl,$pt["lat"],$pt["lon"]);
				}

				if ($rteptcount < $rteptmax - 1) {
					$rteptcount = $rteptmax - 1;
					$pt = $gpxdata["rte".$rtecount]["rtept".$rteptcount];
					$googlemaps->addPolylinePoint($pl,$pt["lat"],$pt["lon"]);
				}
			}
		}
		if (($params->gpxView == "all") || ($params->gpxView == "trk")) {
			switch ($params->measurement) {
				case "imperial":
					$divident = 1.609344;
					break;
				case "nautic_imperial":
				case "nautic_metric":
					$divident = 1.852216;
					break;
				default:
					$divident = 1;
					break;
			}
			$trkmax = $gpxdata["trk_count"];
			$dist = 0;
			$nextcheckpointnumber=1;
			$lastpt = "";
			for ($trkcount=0;$trkcount<$trkmax; $trkcount++) {
				$trkptmax=$gpxdata["trk".$trkcount]["trkpt_count"];
				$pl = $googlemaps->addPolyline($params->gpxLineColor,$params->gpxLineWidth,$params->gpxLineOpacity);
				for ($trkptcount=0;$trkptcount<$trkptmax; $trkptcount+=$params->gpxInterval) {
					$pt = $gpxdata["trk".$trkcount]["trkpt".$trkptcount];
					$googlemaps->addPolylinePoint($pl,$pt["lat"],$pt["lon"]);
					if (($params->gpxCheckpointInterval>0)) {
						if (($trkcount > 0) || ($trkptcount>0)) {
							$dist += myFunctions::distBetween2PointsKM(floatval($lastpt["lat"]),floatval($lastpt["lon"]),floatval($pt["lat"]),floatval($pt["lon"]));
							if ($dist/$divident>$nextcheckpointnumber*$params->gpxCheckpointInterval) {
								$googlemaps->addMarker($pt["lat"],$pt["lon"],"","","","icons/mm_20_".$params->gpxCheckpointMarkerColor.".png","icons/mm_20_shadow.png");
								$nextcheckpointnumber = floor(($dist/$divident) / $params->gpxCheckpointInterval) + 1;
							}
						} else {
							$googlemaps->addMarker($pt["lat"],$pt["lon"],"","","","icons/mm_20_".$params->gpxCheckpointMarkerColor."_s.png","icons/mm_20_shadow.png");
						}
					}
					$lastpt = $pt;
				}
				if ($trkptcount < $trkptmax - 1) {
					$trkptcount = $trkptmax - 1;
					$pt = $gpxdata["trk".$trkcount]["trkpt".$trkptcount];
					$googlemaps->addPolylinePoint($pl,$pt["lat"],$pt["lon"]);
				}
			}
			if (($params->gpxCheckpointInterval>0) && ($trkmax>0)) {
				$googlemaps->addMarker($pt["lat"],$pt["lon"],"","","","icons/mm_20_".$params->gpxCheckpointMarkerColor."_e.png","icons/mm_20_shadow.png");
			}
		}
		if ($params->manualZoom == 4) {
			$googlemaps->clearCenterCoords();
			$googlemaps->adjustCenterCoords($params->maxLon,$params->maxLat);
			$googlemaps->adjustCenterCoords($params->minLon,$params->minLat);
		}
		$googlemaps->setZoom($params->zoom);
		$googlemaps->setDeltaZoom($params->deltazoom);
		if (strlen($error)==0) {
			$output .= $googlemaps->getContentElement().''."\n";;
		}
	} else {
		//KML Files
		if (isset($_GET["kmlid"])) {
			$url = dbFunctions::getPath($id);
			if (strtolower($params->kmlParsing) != "disabled") {
				require_once(dirname(__FILE__).'/xmlgooglemaps_kmlParser.php');
				$kmlfile = new kmlParser();
				$kmlfile->parse($url);

				$kmldata = $kmlfile->output;

				for ($i=0; $i<count($kmldata["coordinates"]); $i++)
				{
					$googlemaps->adjustCenterCoords($kmldata["coordinates"][$i]["lon"],$kmldata["coordinates"][$i]["lat"]);
				}
			}

			if ($params->manualZoom == 4) {
				$googlemaps->clearCenterCoords();
				$googlemaps->adjustCenterCoords($params->maxLon,$params->maxLat);
				$googlemaps->adjustCenterCoords($params->minLon,$params->minLat);
			}

			$file = dbFunctions::getPath($id);
			$googlemaps->setZoom($params->zoom);
			$googlemaps->setDeltaZoom($params->deltazoom);
			if (strlen($error)==0) {
				$output .= $googlemaps->getKMLContentElementOnly($file).''."\n";;
			}
		} else {
			//My Google Maps
			if (isset($_GET["mygooglemapid"])) {
				if ($params->manualZoom == 4) {
					$googlemaps->clearCenterCoords();
					$googlemaps->adjustCenterCoords($params->maxLon,$params->maxLat);
					$googlemaps->adjustCenterCoords($params->minLon,$params->minLat);
				}
				$file = dbFunctions::getPath($id);
				$googlemaps->setZoom($params->zoom);
				$googlemaps->setDeltaZoom($params->deltazoom);
				if (strlen($error)==0) {
					$output .= $googlemaps->getKMLContentElementOnly($file).''."\n";;
				}
			} else {
				//XMLGM and other links
				if (isset($_GET["myid"])) {
					if ($params->manualZoom == 4) {
						$googlemaps->clearCenterCoords();
						$googlemaps->adjustCenterCoords($params->maxLon,$params->maxLat);
						$googlemaps->adjustCenterCoords($params->minLon,$params->minLat);
					}
					$path = dbFunctions::getPath($id);
					$googlemaps->setZoom($params->zoom);
					$googlemaps->setDeltaZoom($params->deltazoom);
					if (strlen($error)==0) {
						if (strlen($path)>0) {
							$output .= $googlemaps->getKMLContentElementOnly($path).''."\n";;
						} else {
							$output .= $googlemaps->getContentElement().''."\n";;
						}
					}
				}
			}
		}
	}
	if (strlen($error)>0) {
		$output .= '<p style="color: red; margin-left: 24px; text-align: justify;"><img alt="Error" src="icons/error48.png" style="float: left; margin-right: 10px;"><img alt="Error" src="icons/error48.png" style="float: right; margin-left: 10px; ">'.$error.'</p>'."\n";;
	}
	$output .= '</body>'."\n";;
	$output .= '</html>'."\n";;

	dbFunctions::setCache($id,$cacheKey,$output);	
	echo $output;
} else {
	echo $cache;
}

	?>